package com.dehui.traders.ui.stockdetail.chart;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.dehui.traders.R;
import com.dehui.traders.bean.BaseListResponse;
import com.dehui.traders.bean.HistData;
import com.dehui.traders.kchart.DataParse;
import com.dehui.traders.kchart.KLineManager;
import com.dehui.traders.kchart.MyCombinedChart;
import com.dehui.traders.service.CallListener;
import com.dehui.traders.service.NetImpl;
import com.dehui.traders.ui.stockdetail.StocksDetailActivity;
import com.dehui.traders.ui.OptionHelper;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.google.gson.Gson;

import java.util.Collections;
import java.util.List;

import retrofit2.Call;
import retrofit2.Response;

public class FenKDetailFragment extends Fragment {


    private View view;
    private MyCombinedChart mChartKline;
    private MyCombinedChart mChartKVolume;
    private String code;
    private String market;
    private String close;
    private int fen;
    private DataParse mData;
    private View llK;
    private View rlLoading;
    private StocksDetailActivity mActivity;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        if (view == null)
            view = inflater.inflate(R.layout.fragment_detail_k,null);
        else{
            ViewGroup parent = (ViewGroup) view.getParent();
            if (parent != null)
                parent.removeView(view);
        }

        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Bundle bundle = getArguments();
        code = bundle.getString("code");
        market = bundle.getString("market");
        close = bundle.getString("close");
        fen = bundle.getInt("fen");
        mActivity = (StocksDetailActivity) getActivity();
        initViews();
        initLesionr();
        getHistData();
    }

    public void getHistData() {
        List<HistData> kHist = OptionHelper.getKHist(getActivity(), code, fen);
        if (kHist != null){
            initHistDataLine(kHist);
        }
        Call<BaseListResponse<HistData>> call = NetImpl.getHistData(market + code, String.valueOf(fen));
        call.enqueue(new CallListener<BaseListResponse<HistData>>() {
            @Override
            public void _onResponse(Response<BaseListResponse<HistData>> response) {
                BaseListResponse<HistData> baseListResponse = response.body();
                if (baseListResponse.status == 200){
                    List<HistData> list = baseListResponse.result;
                    Collections.reverse(list);
                    OptionHelper.saveKHistLine(getActivity(),code,fen,new Gson().toJson(list));
                    initHistDataLine(list);
                }
            }

            @Override
            public void _onFailure() {

            }
        });
    }

    private void initViews() {
        mChartKline = view.findViewById(R.id.mycombinedchart_k_line);
        mChartKVolume = view.findViewById(R.id.mycombinedchart_k_volume);
        llK = view.findViewById(R.id.ll_k);
        rlLoading = view.findViewById(R.id.rl_loading);
    }


    public void initHistDataLine(List<HistData> list) {
        llK.setVisibility(View.VISIBLE);
        rlLoading.setVisibility(View.GONE);
        mData = new DataParse();
        mData.parseKLineHistory(list);
        KLineManager.getInstange().initKLine(getContext(),mChartKline, mChartKVolume, mData);
    }

    public void setChangeFen(int fen) {
        this.fen = fen;
        getHistData();
    }


    private void initLesionr() {
        mChartKline.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
            @Override
            public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
                Highlight highlight = new Highlight(h.getXIndex(), h.getValue(), h.getDataIndex(), h.getDataSetIndex());

                float touchY = h.getTouchY() - mChartKline.getHeight();
                Highlight h1 = mChartKVolume.getHighlightByTouchPoint(h.getXIndex(), touchY);
                highlight.setTouchY(touchY);
                if (null == h1) {
                    highlight.setTouchYValue(0);
                } else {
                    highlight.setTouchYValue(h1.getTouchYValue());
                }
                mChartKVolume.highlightValues(new Highlight[]{highlight});
                mActivity.showKView(h.getXIndex(),mData);
            }

            @Override
            public void onNothingSelected() {
                mActivity.dimissKView();
                mChartKVolume.highlightValue(null);
            }
        });

        mChartKVolume.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
            @Override
            public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
                Highlight highlight = new Highlight(h.getXIndex(), h.getValue(), h.getDataIndex(), h.getDataSetIndex());

                float touchY = h.getTouchY() + mChartKline.getHeight();
                Highlight h1 = mChartKline.getHighlightByTouchPoint(h.getXIndex(), touchY);
                highlight.setTouchY(touchY);
                if (null == h1) {
                    highlight.setTouchYValue(0);
                } else {
                    highlight.setTouchYValue(h1.getTouchYValue());
                }
                mChartKline.highlightValues(new Highlight[]{highlight});
                mActivity.showKView(h.getXIndex(),mData);
            }

            @Override
            public void onNothingSelected() {
                mActivity.dimissKView();
                mChartKline.highlightValue(null);
            }
        });
    }

}
